import { fileURLToPath, URL } from 'node:url';
const path = require('path');

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill';
import rollupNodePolyFill from 'rollup-plugin-node-polyfills';

console.log(import.meta.url);
console.log(process.cwd());
console.log(__dirname);
console.log(path.resolve('01.js'));
console.log(path.join('b', 'c'));

// https://vite.dev/config/
export default defineConfig({
    plugins: [vue()],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url)),
            // 为 Node.js 模块提供浏览器 polyfill
            util: 'rollup-plugin-node-polyfills/polyfills/util',
            buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6',
            stream: 'rollup-plugin-node-polyfills/polyfills/stream',
            url: 'rollup-plugin-node-polyfills/polyfills/url',
        },
    },
    optimizeDeps: {
        esbuildOptions: {
            // 为 Node.js 全局变量提供 polyfill
            plugins: [
                NodeGlobalsPolyfillPlugin({
                    buffer: true,
                }),
                NodeModulesPolyfillPlugin(),
            ],
        },
    },

    build: {
        rollupOptions: {
            plugins: [
                // 为 Rollup 提供 Node.js 模块 polyfill
                rollupNodePolyFill(),
            ],
        },
    },
    server: {
        host: '0.0.0.0',
        port: '3000',
        // open: true,
        proxy: {
            '/api': {
                target: 'http://localhost:8080/',
            },
        },
    },
});
